package JavaCore.streams;

import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *
 */
public class CreatingStreams {

	public static <T> void show(String title, Stream<T> stream) {
		final int SIZE = 10;
		List<T> firstElements = stream.limit(SIZE + 1).collect(Collectors.toList());
		System.out.println(title + ":");
		for (int i = 0; i < firstElements.size(); i++) {
			if (i > 0) {
				System.out.println(", ");
			}
			if (i < SIZE) {
				System.out.println(firstElements.get(i));
			} else {
				System.out.println("....");
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {
		// String[] strArray = { "hello", "word", "!!!" };
		// Stream<String> stream = Stream.of(strArray);
		// Stream<String> silence = Stream.empty();
		Stream<String> song = Stream.of("gently", "down", "the", "stream");
		show("song", song);

		Stream<String> silence = Stream.empty();
		show("silence", silence);

		Stream<String> echos = Stream.generate(() -> "echo");
		show("echos", echos);

		Stream<Double> randoms = Stream.generate(Math::random);
		show("randoms", randoms);

		Stream<BigInteger> integers = Stream.iterate(BigInteger.ONE, n -> n.add(BigInteger.ONE));
		show("integers", integers);
	}

}
